const userDao = require('../dao/userDao.js');
const fileDao = require('../dao/fileDao.js');

const userService = {
    async getUserInfo(uid) {
        let result;
        try {
            result = await userDao.getUserInfo(uid);
            if (result.length) {
                result = result[0];
                let tmp = await fileDao.getAvatarUrl(uid);
                if (tmp.success) {
                    result.avatar = tmp.url;
                } else {
                    result.avatar = '';
                }
                result = { code: 200, msg: '获取用户信息成功', data: result };
            } else {
                result = { code: 500, msg: '用户不存在', data: null };
            }
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async updateUserInfo(uid, sex, grade, college, subCollege, major, username, bio) {
        let result;
        try {
            result = await userDao.updateUserInfo(uid, sex, grade, college, subCollege, major, username, bio);
            result = { code: 200, msg: '修改成功', data: null };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async followCount(uid) {
        let result;
        try {
            result = await userDao.followCount(uid);
            result = { code: 200, msg: '获取成功', data: result[0] };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async follow(uid, followuid, is_like) {
        let result;
        try {
            result = await userDao.follow(uid, followuid, is_like);
            if (is_like) {
                result = { code: 200, msg: '关注成功', data: null };
            } else {
                result = { code: 200, msg: '取消关注成功', data: null };
            }
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async getFollowList(uid, cursor, limit) {
        let result;
        let nextCursor = null;
        try {
            result = await userDao.getFollowList(uid, cursor, limit);
            if (result.length > limit) {
                nextCursor = result[result.length - 1].id;
                result = result.slice(0, limit);
            }
            for (let i = 0; i < result.length; i++) {
                // 设置关注状态
                result[i].status = true;
                // 获取用户头像
                let tmp = await fileDao.getAvatarUrl(result[i].uid);
                if (tmp.success) {
                    result[i].avatar = tmp.url;
                } else {
                    result[i].avatar = '';
                }
            }
            result = { code: 200, msg: '获取关注列表成功', data: { items: result, nextCursor } };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async getFansList(uid, cursor, limit) {
        let result;
        let nextCursor = null;
        try {
            result = await userDao.getFansList(uid, cursor, limit);
            if (result.length > limit) {
                nextCursor = result[result.length - 1].id;
                result = result.slice(0, limit);
            }
            for (let i = 0; i < result.length; i++) {
                // 获取用户头像
                let tmp = await fileDao.getAvatarUrl(result[i].uid);
                if (tmp.success) {
                    result[i].avatar = tmp.url;
                } else {
                    result[i].avatar = '';
                }
            }
            result = { code: 200, msg: '获取关注列表成功', data: { items: result, nextCursor } };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async updateAvatar(uid, buffer) {
        let result;
        try {
            result = await userDao.uploadAvatar(uid, buffer);
            result = { code: 200, msg: '上传头像成功', data: null };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    }
};

module.exports = userService;